package com.ego.edu.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ego.common.utils.R;
import com.ego.edu.entity.Teacher;
import com.ego.edu.entity.vo.TeacherQuery;
import com.ego.edu.service.TeacherService;
import com.ego.servicebase.exceptionhandler.EgoException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author BuYuEgo
 * @since 2021-08-24
 */
@Api(tags="讲师管理")
@RestController
@RequestMapping("/edu/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 查询所有的讲师信息
     * @return
     */
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAll(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    /**
     * 逻辑删除讲师
     * @param id  通过路径传id   http://localhost:8001//edu/teacher/findAll/1 1就是id
     * @return
     */
    @ApiOperation(value = "根据ID逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        boolean flag = teacherService.removeById(id);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }

    }


    @ApiOperation(value = "分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit){

        Page<Teacher> teacherPage = new Page<>(current, limit);
        //page方法查询结果会赋值到teacherPage
        teacherService.page(teacherPage, null);

        try {
            final int i = 10 / 0;
        } catch (Exception e) {
            //执行自定义异常
            throw new EgoException(99999,"执行了自定义异常");
        }

        final long total = teacherPage.getTotal();
        final List<Teacher> records = teacherPage.getRecords();

//        Map map = new HashMap<>();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);

        return R.ok().data("total",total).data("rows",records);

    }

    @ApiOperation(value = "多条件分页查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {

        Page<Teacher> teacherPage = new Page<>(current, limit);

        final QueryWrapper<Teacher> wrapper = Wrappers.query();
        wrapper.like(StringUtils.isNotBlank(teacherQuery.getName()), "name", teacherQuery.getName())
                .eq(teacherQuery.getLevel() != null,"level", teacherQuery.getLevel())
                .le(StringUtils.isNotBlank(teacherQuery.getEnd()), "gmt_create", teacherQuery.getEnd())
                .ge(StringUtils.isNotBlank(teacherQuery.getBegin()), "gmt_create", teacherQuery.getBegin());

        teacherService.page(teacherPage,wrapper);

        final long total = teacherPage.getTotal();
        final List<Teacher> records = teacherPage.getRecords();
        return R.ok().data("total",total).data("rows",records);

    }

    /**
     * 添加讲师
     * @param teacher
     * @return
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teacher teacher) {
        final boolean save = teacherService.save(teacher);
        return save ? R.ok() : R.error();
    }

    /**
     * 根据id查询讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacherByID(@PathVariable String id){
        final Teacher teacher = teacherService.getById(id);
        return R.ok().data("taacher", teacher);
    }

    /**
     * 根据id修改讲师
     * @param teacher
     * @return
     */
    @ApiOperation(value = "根据id修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher){
        final boolean update = teacherService.updateById(teacher);
        return update ? R.ok() : R.error();
    }


}

